package fragments;

import java.util.HashMap;

import mindshare.ProfileInf;
import mindshare.SessionManager;

import org.json.JSONException;

import fragments.Question;
import info.androidhive.slidingmenu.Answer;
import info.androidhive.slidingmenu.R;
import Agents.FilterQuestions;
import Agents.Publicity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

public class HomeFragment extends Fragment {
	
	private View rootView;
	private EditText question;
	private ImageView adView;
	MenuItem refreshMenuItem;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_home, container, false);
		question = (EditText) rootView.findViewById(R.id.question);
		question.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					goQuestion();
					question.clearFocus();
				}
				
			}
		});
		setHasOptionsMenu(true);
		
		
		adView = (ImageView)rootView.findViewById(R.id.ad);
		Publicity advertisement = new Publicity();
//		int x = Integer.parseInt(advertisement.chooseAd());
		adView.setImageResource(R.drawable.ad + advertisement.chooseAd());
		return rootView;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.empty_menu, menu);
		refreshMenuItem = menu.findItem(R.id.empty_sea);
		try{
			test();
		}catch(NullPointerException e){
			e.printStackTrace();
		}
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	public void test(){
		SessionManager s=new SessionManager(rootView.getContext());
		HashMap<String, String> user=s.getUserDetails();
		String nickname=user.get(SessionManager.KEY_NAME);
		ProfileInf p = new ProfileInf(nickname, rootView.getContext());
		FilterQuestions fq = new FilterQuestions(rootView.getContext(), refreshMenuItem);
		Log.d("home", "probando");
		try {
			fq.getQuestionsIds();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		//Post p = new Post(17,rootView.getContext());
		//Log.d("Inf", "Post id:"+p.getPostId());
		//Log.d("Inf", "Post category:"+p.getCategory());
		//Log.d("Inf", "Post name:"+p.getName());
		
	}

	
	public void goQuestion(){
		Intent intention = new Intent(rootView.getContext(), Question.class);
		this.startActivity(intention);
	}
	
	public void goAnswer() {
		Intent intention = new Intent(rootView.getContext(), Answer.class);
		this.startActivity(intention);
	}
}

